#include "../header.h"



//1.套接字
//2.修改属性。
//3.发送
//4.关闭套接字文件

int main(int argc, char const *argv[])
{
    int fid = socket(AF_INET,SOCK_DGRAM,0);
    printf("fid = %d\n",fid);
    //修改属性
    //!----------------------广播属性设定代码部分---------------------//
    //2.修改属性
    int value = 0;
    int size = sizeof(value);
    getsockopt(fid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    //设置属性
    value = 1;
    setsockopt(fid,SOL_SOCKET,SO_BROADCAST,&value,size);
    getsockopt(fid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    printf("value = %d\n",value);
    //!--------------------------------------------------------//
    //直到对面的地址才能发送。不关心地址ip但是关心 端口。
    struct sockaddr_in faddr = {0};
    faddr.sin_addr.s_addr = INADDR_BROADCAST;
    faddr.sin_port = htons(atoi(argv[1]));
    faddr.sin_family = AF_INET;
    char bufv[128] = "";

    //再来一个成员，发送。
    struct sockaddr_in kaddr = {0};
    kaddr.sin_addr.s_addr = INADDR_BROADCAST;
    kaddr.sin_port = htons(atoi(argv[2]));
    kaddr.sin_family = AF_INET;

    while(1)
    {
        printf("请输入要广播的信息:");
        scanf("%s",bufv);
        sendto(fid,bufv,strlen(bufv),0,(struct sockaddr*)&faddr,sizeof(faddr));
        sendto(fid,bufv,strlen(bufv),0,(struct sockaddr*)&kaddr,sizeof(kaddr));
        printf("数据已经发送!\n");
        bzero(bufv,sizeof(bufv));
    }   

    close(fid);
    return 0;
}
